```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java 优先级队列(PriorityQueue)详解 | 技术小馆</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background-color: #f8fafc;
            color: #1e293b;
            line-height: 1.6;
        }
        .hero {
            background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
        }
        .card {
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }
        .card:hover {
            transform: translateY(-4px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .code-block {
            background-color: #1e293b;
            border-radius: 0.375rem;
            font-family: 'Courier New', Courier, monospace;
        }
        .first-letter {
            font-size: 4rem;
            line-height: 1;
            float: left;
            margin-right: 0.5rem;
            color: #4f46e5;
            font-weight: bold;
        }
        .mermaid {
            background-color: white;
            padding: 1.5rem;
            border-radius: 0.5rem;
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero text-white py-20 px-4 md:py-24">
        <div class="container mx-auto max-w-5xl text-center">
            <h1 class="text-4xl md:text-5xl font-bold mb-4 font-serif">深入理解 Java 优先级队列</h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90">探索 PriorityQueue 的内部机制与应用场景</p>
            <div class="flex justify-center space-x-4">
                <span class="px-4 py-2 bg-white text-indigo-600 rounded-full font-medium">数据结构</span>
                <span class="px-4 py-2 bg-white text-indigo-600 rounded-full font-medium">Java 集合</span>
                <span class="px-4 py-2 bg-white text-indigo-600 rounded-full font-medium">算法优化</span>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto max-w-5xl px-4 py-12">
        <!-- Introduction -->
        <section class="mb-16">
            <p class="text-xl leading-relaxed mb-6">
                <span class="first-letter">J</span>ava 中的优先级队列（<code class="bg-indigo-100 text-indigo-800 px-1 rounded">PriorityQueue</code>）是一个基于优先级的队列数据结构，它是 <code class="bg-indigo-100 text-indigo-800 px-1 rounded">java.util</code> 包中的一个重要类。<code class="bg-indigo-100 text-indigo-800 px-1 rounded">PriorityQueue</code> 实现了 <code class="bg-indigo-100 text-indigo-800 px-1 rounded">Queue</code> 接口，并提供了基于元素优先级的排序功能，在算法实现、任务调度等场景中发挥着关键作用。
            </p>
            
            <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-12">
                <div class="card bg-white p-6 rounded-xl shadow-md border-l-4 border-indigo-500">
                    <h3 class="text-xl font-bold mb-3 flex items-center">
                        <i class="fas fa-lightbulb text-indigo-500 mr-2"></i> 核心特点
                    </h3>
                    <ul class="space-y-2">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span>基于优先级自动排序</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span>动态扩容，无固定容量限制</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span>使用二叉堆数据结构实现</span>
                        </li>
                    </ul>
                </div>
                
                <div class="card bg-white p-6 rounded-xl shadow-md border-l-4 border-blue-500">
                    <h3 class="text-xl font-bold mb-3 flex items-center">
                        <i class="fas fa-cogs text-blue-500 mr-2"></i> 性能表现
                    </h3>
                    <ul class="space-y-2">
                        <li class="flex items-start">
                            <i class="fas fa-bolt text-yellow-500 mt-1 mr-2"></i>
                            <span>插入操作: <code class="bg-gray-100 px-1 rounded">O(log n)</code></span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-bolt text-yellow-500 mt-1 mr-2"></i>
                            <span>取出操作: <code class="bg-gray-100 px-1 rounded">O(log n)</code></span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-bolt text-yellow-500 mt-1 mr-2"></i>
                            <span>查看堆顶: <code class="bg-gray-100 px-1 rounded">O(1)</code></span>
                        </li>
                    </ul>
                </div>
            </div>
        </section>

        <!-- Basic Concepts -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-6 font-serif flex items-center">
                <i class="fas fa-cube text-indigo-500 mr-3"></i>
                <span>1. 优先级队列的基本概念</span>
            </h2>
            <div class="bg-white rounded-xl shadow-md overflow-hidden">
                <div class="p-6">
                    <p class="mb-4">优先级队列是一种特殊的队列，其中每个元素都有一个优先级。队列中的元素是按照优先级顺序进行处理的，优先级高的元素会先被取出。与普通队列不同，优先级队列不会按照元素的插入顺序(FIFO)进行处理，而是根据优先级决定处理顺序。</p>
                    
                    <div class="flex flex-col md:flex-row gap-6 mt-6">
                        <div class="flex-1">
                            <h3 class="text-xl font-semibold mb-3 text-indigo-700 flex items-center">
                                <i class="fas fa-random mr-2"></i>普通队列 (FIFO)
                            </h3>
                            <div class="bg-gray-100 p-4 rounded-lg">
                                <div class="flex justify-center space-x-2 mb-4">
                                    <div class="w-10 h-10 bg-blue-500 rounded flex items-center justify-center text-white font-bold">A</div>
                                    <div class="w-10 h-10 bg-blue-500 rounded flex items-center justify-center text-white font-bold">B</div>
                                    <div class="w-10 h-10 bg-blue-500 rounded flex items-center justify-center text-white font-bold">C</div>
                                </div>
                                <p class="text-center">处理顺序: A → B → C</p>
                            </div>
                        </div>
                        
                        <div class="flex-1">
                            <h3 class="text-xl font-semibold mb-3 text-indigo-700 flex items-center">
                                <i class="fas fa-sort-amount-down mr-2"></i>优先级队列
                            </h3>
                            <div class="bg-gray-100 p-4 rounded-lg">
                                <div class="flex justify-center space-x-2 mb-4">
                                    <div class="w-10 h-10 bg-purple-500 rounded flex items-center justify-center text-white font-bold">3</div>
                                    <div class="w-10 h-10 bg-purple-500 rounded flex items-center justify-center text-white font-bold">1</div>
                                    <div class="w-10 h-10 bg-purple-500 rounded flex items-center justify-center text-white font-bold">2</div>
                                </div>
                                <p class="text-center">处理顺序: 1 → 2 → 3</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Features -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-6 font-serif flex items-center">
                <i class="fas fa-star text-indigo-500 mr-3"></i>
                <span>2. PriorityQueue 的特点</span>
            </h2>
            
            <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                <div class="card bg-white p-6 rounded-xl shadow-md">
                    <h3 class="text-xl font-bold mb-3 text-indigo-700 flex items-center">
                        <i class="fas fa-sort-amount-down-alt mr-2"></i>自动排序
                    </h3>
                    <p><code class="bg-indigo-100 text-indigo-800 px-1 rounded">PriorityQueue</code> 内部会自动根据元素的自然顺序或者自定义的比较器进行排序。最小值（或最大值）会在队列的前面。</p>
                </div>
                
                <div class="card bg-white p-6 rounded-xl shadow-md">
                    <h3 class="text-xl font-bold mb-3 text-indigo-700 flex items-center">
                        <i class="fas fa-expand mr-2"></i>无容量限制
                    </h3>
                    <p><code class="bg-indigo-100 text-indigo-800 px-1 rounded">PriorityQueue</code> 默认不限制容量，会动态扩容，但受限于系统内存。</p>
                </div>
                
                <div class="card bg-white p-6 rounded-xl shadow-md">
                    <h3 class="text-xl font-bold mb-3 text-indigo-700 flex items-center">
                        <i class="fas fa-ban mr-2"></i>不支持 null 元素
                    </h3>
                    <p>队列中不能包含 <code class="bg-indigo-100 text-indigo-800 px-1 rounded">null</code> 元素，否则会抛出 <code class="bg-red-100 text-red-800 px-1 rounded">NullPointerException</code>。</p>
                </div>
                
                <div class="card bg-white p-6 rounded-xl shadow-md">
                    <h3 class="text-xl font-bold mb-3 text-indigo-700 flex items-center">
                        <i class="fas fa-user-shield mr-2"></i>非线程安全
                    </h3>
                    <p><code class="bg-indigo-100 text-indigo-800 px-1 rounded">PriorityQueue</code> 不是线程安全的。如果需要在多线程环境中使用，需要使用外部同步机制。</p>
                </div>
            </div>
        </section>

        <!-- Implementation -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-6 font-serif flex items-center">
                <i class="fas fa-layer-group text-indigo-500 mr-3"></i>
                <span>3. 内部实现原理</span>
            </h2>
            
            <div class="bg-white rounded-xl shadow-md overflow-hidden mb-8">
                <div class="p-6">
                    <p class="mb-4"><code class="bg-indigo-100 text-indigo-800 px-1 rounded">PriorityQueue</code> 是基于 <strong>二叉堆（Binary Heap）</strong> 实现的。具体来说，它通常使用 <strong>小顶堆（Min-Heap）</strong> 作为内部数据结构，保证了堆顶元素是队列中最小的元素（可以通过比较器实现最大堆）。</p>
                    
                    <h3 class="text-xl font-semibold mb-3 mt-6 text-indigo-700">二叉堆的特点</h3>
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                        <div>
                            <h4 class="font-medium mb-2 flex items-center">
                                <i class="fas fa-project-diagram mr-2"></i>完全二叉树结构
                            </h4>
                            <p>堆是一种完全二叉树，因此所有层都是完全填满的，除了最后一层可能不满。</p>
                        </div>
                        <div>
                            <h4 class="font-medium mb-2 flex items-center">
                                <i class="fas fa-sort-numeric-down mr-2"></i>堆序性质
                            </h4>
                            <p>对于小顶堆，父节点的值总是不大于其子节点的值。这样，最小值总是位于堆顶。</p>
                        </div>
                    </div>
                    
                    <div class="mt-8">
                        <h3 class="text-xl font-semibold mb-4 text-indigo-700 flex items-center">
                            <i class="fas fa-sitemap mr-2"></i>二叉堆可视化
                        </h3>
                        <div class="mermaid">
                            graph TD
                                A((1)) --> B((3))
                                A --> C((2))
                                B --> D((5))
                                B --> E((9))
                                C --> F((4))
                                C --> G((7))
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
                <div class="card bg-white p-6 rounded-xl shadow-md">
                    <h3 class="text-xl font-bold mb-3 text-indigo-700 flex items-center">
                        <i class="fas fa-plus-circle mr-2"></i>插入操作
                    </h3>
                    <p class="text-2xl font-bold text-center text-indigo-600">O(log n)</p>
                    <p class="text-center mt-2">将新元素插入堆中并调整堆的结构</p>
                </div>
                
                <div class="card bg-white p-6 rounded-xl shadow-md">
                    <h3 class="text-xl font-bold mb-3 text-indigo-700 flex items-center">
                        <i class="fas fa-minus-circle mr-2"></i>取出操作
                    </h3>
                    <p class="text-2xl font-bold text-center text-indigo-600">O(log n)</p>
                    <p class="text-center mt-2">删除堆顶元素并重新调整堆的结构</p>
                </div>
                
                <div class="card bg-white p-6 rounded-xl shadow-md">
                    <h3 class="text-xl font-bold mb-3 text-indigo-700 flex items-center">
                        <i class="fas fa-eye mr-2"></i>访问堆顶
                    </h3>
                    <p class="text-2xl font-bold text-center text-indigo-600">O(1)</p>
                    <p class="text-center mt-2">直接访问堆顶元素</p>
                </div>
            </div>
        </section>

        <!-- Usage -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-6 font-serif flex items-center">
                <i class="fas fa-code text-indigo-500 mr-3"></i>
                <span>4. PriorityQueue 的使用</span>
            </h2>
            
            <div class="bg-white rounded-xl shadow-md overflow-hidden mb-8">
                <div class="p-6">
                    <h3 class="text-xl font-semibold mb-4 text-indigo-700 flex items-center">
                        <i class="fas fa-plus-square mr-2"></i>创建 PriorityQueue
                    </h3>
                    
                    <div class="code-block p-4 rounded-lg mb-6">
                        <pre class="text-gray-200 overflow-x-auto"><code>import java.util.PriorityQueue;
import java.util.Comparator;

public class PriorityQueueDemo {
    public static void main(String[] args) {
        // 使用自然顺序创建最小堆
        PriorityQueue&lt;Integer&gt; minHeap = new PriorityQueue&lt;&gt;();
        
        // 使用自定义比较器创建最大堆
        PriorityQueue&lt;Integer&gt; maxHeap = new PriorityQueue&lt;&gt;(Comparator.reverseOrder());

        minHeap.add(3);
        minHeap.add(1);
        minHeap.add(2);

        System.out.println("最小堆的堆顶元素: " + minHeap.peek()); // 输出1

        maxHeap.add(3);
        maxHeap.add(1);
        maxHeap.add(2);

        System.out.println("最大堆的堆顶元素: " + maxHeap.peek()); // 输出3
    }
}</code></pre>
                    </div>
                    
                    <h3 class="text-xl font-semibold mb-4 text-indigo-700 flex items-center">
                        <i class="fas fa-tasks mr-2"></i>常用操作
                    </h3>
                    
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                        <div class="bg-gray-50 p-4 rounded-lg">
                            <h4 class="font-medium text-indigo-600 mb-2"><code class="bg-indigo-100 text-indigo-800 px-1 rounded">add(E e)</code> / <code class="bg-indigo-100 text-indigo-800 px-1 rounded">offer(E e)</code></h4>
                            <p>将元素插入队列</p>
                        </div>
                        <div class="bg-gray-50 p-4 rounded-lg">
                            <h4 class="font-medium text-indigo-600 mb-2"><code class="bg-indigo-100 text-indigo-800 px-1 rounded">poll()</code></h4>
                            <p>移除并返回队列中的最小元素</p>
                        </div>
                        <div class="bg-gray-50 p-4 rounded-lg">
                            <h4 class="font-medium text-indigo-600 mb-2"><code class="bg-indigo-100 text-indigo-800 px-1 rounded">peek()</code></h4>
                            <p>返回队列中的最小元素但不移除它</p>
                        </div>
                        <div class="bg-gray-50 p-4 rounded-lg">
                            <h4 class="font-medium text-indigo-600 mb-2"><code class="bg-indigo-100 text-indigo-800 px-1 rounded">remove(Object o)</code></h4>
                            <p>删除队列中的指定元素</p>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Use Cases -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-6 font-serif flex items-center">
                <i class="fas fa-lightbulb text-indigo-500 mr-3"></i>
                <span>5. 使用场景</span>
            </h2>
            
            <div class="bg-white rounded-xl shadow-md overflow-hidden">
                <div class="p-6">
                    <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
                        <div class="card p-6 border-l-4 border-blue-500">
                            <h3 class="text-xl font-bold mb-3 flex items-center">
                                <i class="fas fa-tasks text-blue-500 mr-2"></i> 优先级调度
                            </h3>
                            <p>用于任务调度系统，需要根据任务优先级决定处理顺序。</p>
                        </div>
                        
                        <div class="card p-6 border-l-4 border-purple-500">
                            <h3 class="text-xl font-bold mb-3 flex items-center">
                                <i class="fas fa-project-diagram text-purple-500 mr-2"></i> 算法实现
                            </h3>
                            <p>如 Dijkstra 算法、A* 算法等需要处理优先级的数据结构。</p>
                        </div>
                        
                        <div class="card p-6 border-l-4 border-green-500">
                            <h3 class="text-xl font-bold mb-3 flex items-center">
                                <i class="fas fa-stream text-green-500 mr-2"></i> 数据流处理
                            </h3>
                            <p>实时计算中的数据流中需要基于优先级的处理。</p>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Notes -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-6 font-serif flex items-center">
                <i class="fas fa-exclamation-triangle text-indigo-500 mr-3"></i>
                <span>6. 注意事项</span>
            </h2>
            
            <div class="bg-white rounded-xl shadow-md overflow-hidden">
                <div class="p-6">
                    <div class="space-y-6">
                        <div class="flex items-start">
                            <div class="flex-shrink-0 h-10 w-10 rounded-full bg-red-100 flex items-center justify-center text-red-500 mr-4">
                                <i class="fas fa-exclamation"></i>
                            </div>
                            <div>
                                <h3 class="text-lg font-semibold mb-1">元素的比较</h3>
                                <p><code class="bg-indigo-100 text-indigo-800 px-1 rounded">PriorityQueue</code> 需要元素实现 <code class="bg-indigo-100 text-indigo-800 px-1 rounded">Comparable</code> 接口或提供自定义的 <code class="bg-indigo-100 text-indigo-800 px-1 rounded">Comparator</code>。否则，插入和排序操作会抛出 <code class="bg-red-100 text-red-800 px-1 rounded">ClassCastException</code>。</p>
                            </div>
                        </div>
                        
                        <div class="flex items-start">
                            <div class="flex-shrink-0 h-10 w-10 rounded-full bg-yellow-100 flex items-center justify-center text-yellow-500 mr-4">
                                <i class="fas fa-tachometer-alt"></i>
                            </div>
                            <div>
                                <h3 class="text-lg font-semibold mb-1">性能考虑</h3>
                                <p>虽然 <code class="bg-indigo-100 text-indigo-800 px-1 rounded">PriorityQueue</code> 提供了高效的插入和删除操作，但如果需要频繁地访问中间元素或者进行其他非标准操作，可能需要考虑其他数据结构。</p>
                            </div>
                        </div>
                        
                        <div class="flex items-start">
                            <div class="flex-shrink-0 h-10 w-10 rounded-full bg-blue-100 flex items-center justify-center text-blue-500 mr-4">
                                <i class="fas fa-users"></i>
                            </div>
                            <div>
                                <h3 class="text-lg font-semibold mb-1">线程安全</h3>
                                <p><code class="bg-indigo-100 text-indigo-800 px-1 rounded">PriorityQueue</code> 不是线程安全的。在多线程环境中，可以使用 <code class="bg-indigo-100 text-indigo-800 px-1 rounded">PriorityBlockingQueue</code> 或手动同步。</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-8">
        <div class="container mx-auto max-w-5xl px-4 text-center">
            <h3 class="text-xl font-bold mb-2">技术小馆</h3>
            <p class="mb-4">探索编程世界的无限可能</p>
            <a href="http://www.yuque.com/jtostring" class="text-indigo-400 hover:text-indigo-300 transition-colors">
                <i class="fas fa-globe mr-1"></i> http://www.yuque.com/jtostring
            </a>
        </div>
    </footer>

    <!-- Mermaid JS -->
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: false,
                htmlLabels: true,
                curve: 'basis'
            }
        });
        
        // 添加卡片悬停效果
        document.querySelectorAll('.card').forEach(card => {
            card.addEventListener('mouseenter', function() {
                this.style.transform = 'translateY(-4px)';
                this.style.boxShadow = '0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04)';
            });
            
            card.addEventListener('mouseleave', function() {
                this.style.transform = '';
                this.style.boxShadow = '';
            });
        });
    </script>
</body>
</html>
```